package com.project.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.project.bean.kaoshiUserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/8/2 11:04
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    IUserService service;

    @RequestMapping("add")
    public String add(@Valid kaoshiUserBean kaoshiUserBean, BindingResult result) throws JsonProcessingException {
        if (result.hasErrors()){
            List<FieldError> list=result.getFieldErrors();
            String str=new ObjectMapper().writeValueAsString(list);
            return str;
        }
        service.add(kaoshiUserBean);
        return "ok";
    }

    @RequestMapping("findByItem")
    public PageInfo<kaoshiUserBean> findByItem(int pageNo){
        return service.findByItem(pageNo);
    }
}
